﻿using System.Windows;
using System.Windows.Media;

namespace Red.Core.Extensions
{
    public static class WindowExt
    {
        public static void SetPositionRelativeTo( this Window window, Visual target, double xOffset = 0, double yOffset = 0 )
        {
            PresentationSource source = PresentationSource.FromVisual( window );

            if( source == null )
            {
                source = PresentationSource.FromVisual( target );
            }

            double dpiX = source.CompositionTarget.TransformToDevice.M11;
            double dpiY = source.CompositionTarget.TransformToDevice.M22;

            Point location = target.PointToScreen( new Point( 0, 0 ) );
            window.Left = ( location.X + ( xOffset * dpiX ) ) / dpiX;
            window.Top = ( location.Y + ( yOffset * dpiY ) ) / dpiY;
        }
    }
}
